//Goals
////1. load an external sprite and move him around the screen
/*
$(document).ready(function(){
	
	Crafty.init(840, 630);
	
	Crafty.sprite(42,'data/sergeant.png',{marine : [0,0]});
	
	var c = Crafty.e("2D, Canvas, Fourway, marine")
		.fourway(10);
	
	console.log(c)
		
});
*/ 

////2. turn the sprite around its center axis with the Q/E arrow keys
/*
$(document).ready(function(){
	
	Crafty.init(840, 630);
	
	Crafty.sprite(42,'data/sergeant.png',{marine : [0,0]});
	
	var c = Crafty.e("2D, Canvas, Fourway, KeyBoard, marine")
		.fourway(10)
		.origin("center")
		.bind("KeyUp", function(e){ 
			if(e.keyCode === Crafty.keys.Q) {
				//turn left
				this.rotation -= 90;
			} else if(e.keyCode === Crafty.keys.E) {
				//turn right			
				this.rotation += 90;
			}
		});

	console.log(c)
		
});
*/

////3. Load a tilemap
/*
$(document).ready(function(){
	
	Crafty.init(840, 630);
	
	Crafty.e("TiledLevel").tiledLevel("data/testLevel.json","DOM");
		
});
*/

////4. Load a character onto the tilemap
/*
$(document).ready(function(){
	Crafty.init(840, 630);
	Crafty.canvas.init();
    Crafty.background("black");
	
	var map = Crafty.e("TiledLevel").tiledLevel("data/testLevel.json","Canvas");
	Crafty.sprite(42,'data/sergeant.png',{marine : [0,0]});
	
	var c = Crafty.e("2D, Canvas, KeyBoard, marine, Solid")
		.attr({x: 756, y: 42, z: 100})
		//.fourway(10)
		.origin("center")

	console.log(c)
		
});
*/


////5. Load a character onto the tilemap, move the character around with collision detection
    var mvX = 42;
    var mvY = 42;
    var rotL = 90;
    var rotR = 90;

$(document).ready(function(){
	Crafty.init(840, 630);
	Crafty.canvas.init();
    Crafty.background("black");
	var map = Crafty.e("TiledLevel").tiledLevel("data/testLevel.json");
	
	Crafty.sprite(42,'data/sergeant.png',{marine : [0,0]});
	
	var c = Crafty.e("2D, Canvas, KeyBoard, marine")
		.attr({x: 42, y: 42, z: 100})
		//.fourway(10)
		.origin("center")
		.bind("KeyDown", function(e){ controlManager(this, e, map) });
});


controlManager = function(ent, e, map){
	switch(e.keyCode){
		case Crafty.keys.Q:
			//turn left
			ent.rotation -= rotL;
		break;
		case Crafty.keys.E:
			//turn right			
			ent.rotation += rotR;
		break;
		
		case Crafty.keys.W:
            if(canMove('n', ent, map))
                ent.move('n',mvY);
		break;
		
		case Crafty.keys.S:
            if(canMove('s', ent, map))
                ent.move('s',mvY);
		break;
		
		case Crafty.keys.A:
            if(canMove('w', ent, map))
                ent.move('w',mvX); 
		break;
		
		case Crafty.keys.D:
            if(canMove('e', ent, map))
                ent.move('e',mvX);
		break;
	}
}

canMove = function(dir,ent, map) {
    //Find the sprites future position and check what sort of tile is there
    var futX = ent._x;
    var futY = ent._y;
    var tile;
    
    switch(dir){
        case 'n':
            futY = ent._y - mvY;
        break;
    
        case 's':
            futY = ent._y + mvY;
        break;
    
        case 'e':
            futX = ent._x + mvX;
        break;
        
        case 'w':
            futX = ent._x - mvX;
        break;
        
        default:
            alert("Invalid direction identifier");
            return false;
    }
     
     //tile = map.getTile(1,2,1);
     console.log("Current position - pixels(x:"+ent._x+" y:"+ent._y+") - coords(X:" +ent._x/mvX+ " Y:" +ent._y/mvY+ ")");
     console.log("Future position - pixels(x:"+futX+" y:"+futY+") - coords(X:" +futX/mvX+ " Y:" +futY/mvY+ ")");
     
     tile = map.getTile(futX/mvX, futY/mvY, 0);
     
     
     if(tile)
        return true;
     else
        return false;
}


